﻿using System;
using System.Collections;
using CSharp.Libs.Net;

/* description of chefkoch.de api found here:
 * 
 * the following is ruby code. it shows how the two main php scripts work, though.
 * http://www.pebra.net/blog/2012/10/31/how-to-get-recipes-fromchefkoch-dot-de/
 * http://www.pebra.net/blog/2013/03/13/Get-recipes-from-chefkoch.de-using-ruby-Part-2/
 * 
 * this one is java. it also shows how the two main php scripts work
 * http://code.google.com/p/recipeminer/source/browse/src/main/java/de/recipeminer/ChefkochAPICrawler.java?r=628f1162ab91f0de45147a199074fcb9ff800167
 */
/* http://www.pebra.net/blog/2012/10/31/how-to-get-recipes-fromchefkoch-dot-de/
require 'net/http'
require 'json'
require 'uri'

module ChefkochApi
  @api_url = "http://api.chefkoch.de/api/1.0/api-recipe-search.php"

  def self.search(search_text)
    params = {}
    params["Suchbegriff"] = search_text.strip
    # params["limit"] = 20 # you can pass the limit amount of results you like to receive

    url = URI.parse @api_url
    url.query = URI.encode_www_form(params)

    response = Net::HTTP.get_response(url)
    response.body
    end
  end
 */

namespace CSharp.Libs.Misc
{
    public class ReceipeSearcher : IDisposable
    {
        #region declarations
        private PostSubmitter _ps;
        #endregion



        #region constructor
        public ReceipeSearcher()
        {
            _ps = new PostSubmitter();
        }

        #region dispose pattern (http://www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-Pattern-P)
        // some fields that require cleanup
        private bool disposed = false; // to detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // clean up managed handles here
                    if (_ps != null) { _ps = null; }
                }

                disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }
        #endregion
        #endregion



        #region private functions
        private void _getSearchResults()
        {
            if (string.IsNullOrEmpty(SearchString))
            {
                return;
            }

            if (_ps == null) { _ps = new PostSubmitter(); }

            _ps.Type = PostSubmitter.PostTypeEnum.Post;
            _ps.Url = @"http://api.chefkoch.de/api/1.0/api-recipe-search.php";
            _ps.PostItems.Add("Suchbegriff", SearchString);

            string strResult = _ps.Post(_ps.Url,_ps.PostItems);

            SearchResultString = strResult;
            SearchResultHashtable = JSON.JsonDecode(strResult) as Hashtable;

            _ps = null;
        }
        #endregion



        #region properties
        public string SearchString { get; set; }

        public string SearchResultString { get; set; }
        public Hashtable SearchResultHashtable { get; set; }
        #endregion



        #region methods
        public void GetSearchResults()
        {
            _getSearchResults();
        }

        public void ClearResults()
        {
            SearchResultString = "";
            SearchResultHashtable = null;

            SearchString = "";
        }
        #endregion
    }
}
